﻿using System.DeviceServer.MyUDP;
using System.Net;

using GCC.Container.LogProviders;

using StringUDPServer = System.DeviceServer.MyUDP.MyUDPServer;
namespace System.DeviceServer
{
    public class DeviceCommManager
    {
        public static DeviceCommManager Instance = new();

        public StringUDPServer Server { get; private set; }
        public Dictionary<string, string> _map = new();


        private DeviceCommManager()
        {

        }
        public void Init()
        {
            var _logger = GCCLoggerProvider.GetInstance(new LogOptions() { }).CreateLogger<DeviceCommManager>();
            //var localIP = new IPEndPoint(IPAddress.Parse("192.168.5.61"), 5020);

            var localIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5022);
            Server = new StringUDPServer(localIP, _logger, "radarService");//写邮寄地址
        }

        public void Start()
        {
            //if (Server is null)
            //{
            //    return;
            //}
            //Console.WriteLine("lsltest DeviceCommManager task entered.");
            //var _logger = GCCLoggerProvider.GetInstance(new LogOptions() { }).CreateLogger<DeviceCommManager>();

            //var cancel = new CancellationTokenSource();
            ////var localIP = new IPEndPoint(IPAddress.Parse("10.10.100.49"), 5022);
            //var localIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5022);

            //var server = new StringUDPServer(localIP, _logger, "radarService");
            //_ = Task.Factory.StartNew(async () =>
            //{
                
            //    server.Start();
            //    var count = 0;


            //    server.GotClient += (client) =>
            //    {
            //        client.GotData += ((data) =>
            //        {
            //            Console.WriteLine($"我是服务端,我收到了:{data}");
            //            count++;
            //            string result = string.Empty;
            //            if (data.Equals("55 AA 05 07 05 A0 00 00 00 00 B1 F8"))
            //            {
            //                int value = DateTime.Now.Minute + DateTime.Now.Second;
            //                //output = Convert.ToString()
            //            }
            //            _ = client.Send("我是服务端你好啊");
            //        });
                    //while (!cancel.Token.IsCancellationRequested)
                    //{

                    //}
                    //Console.WriteLine($"收到一个客户端 Remote:{client.RemotePoint} ");
            //    };

            //}, TaskCreationOptions.LongRunning);

        }

    }
}

